package com.chain.BrotherExchange.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.ui.core.BaseActivity;
import com.chain.BrotherExchange.ui.dialog.SendVerificationCodeDialog;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.utils.Androids;
import com.chain.BrotherExchange.utils.TimeCount;
import com.chain.BrotherExchange.viewmodel.SecurityCenterViewModel;
import com.chain.BrotherExchange.viewmodel.VerificationManage;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import butterknife.Bind;

/**
 * 修改登录密码
 */
public class ModifyLoginPasswordActivity extends BaseActivity {

    @Bind(R.id.affirm_button) View mAffirmButton;
    @Bind(R.id.old_login_password_edit) EditText mOldLoginPasswordEdit;
    @Bind(R.id.new_login_password_edit) EditText mNewLoginPasswordEdit;
    @Bind(R.id.affirm_login_password_edit) EditText mAffirmLoginPasswordEdit;

    private SecurityCenterViewModel mViewModel;

    public static void launch(Context context) {
        context.startActivity(new Intent(context, ModifyLoginPasswordActivity.class));
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_modify_login_password;
    }

    @Override
    protected void initActions() {
        ActivityBarHelper.init(this, R.string.title_modify_password);

        mViewModel = ViewModelProviders.of(this).get(SecurityCenterViewModel.class);

        // 更改密码，发送短信验证码
        mAffirmButton.setOnClickListener(v -> startModifyPassword());
    }

    private void startModifyPassword() {
        String oldPassword = mOldLoginPasswordEdit.getText().toString();
        String newPassword = mNewLoginPasswordEdit.getText().toString();
        String affirmPassword = mAffirmLoginPasswordEdit.getText().toString();

        if (VerificationManage.verifyPassword(oldPassword)
                && VerificationManage.verifyPasswordSame(newPassword, affirmPassword)) {
            SendVerificationCodeDialog.show(
                    this, view -> sendMessage((TextView) view),
                    this::modifyPassword
            );
        }
    }

    private void modifyPassword(SendVerificationCodeDialog dialog) {
        String oldPassword = mOldLoginPasswordEdit.getText().toString();
        String newPassword = mNewLoginPasswordEdit.getText().toString();
        String affirmPassword = mAffirmLoginPasswordEdit.getText().toString();
        mViewModel.modifyLoginPassword(oldPassword, newPassword, affirmPassword, dialog.getCode())
                .observe(this, resource ->
                        Resource.load(this, resource, dialog, result -> {
                            dialog.successNetwork();
                            finish();
                        })
                );
    }

    private void sendMessage(TextView textView) {
        mViewModel.sendMessage(C.API_MODIFY_LOGIN_PASSWORD_TYPE).observe(this, resource -> {
            if (resource.state == Resource.Status.LOADING) {
                textView.setClickable(false);
                textView.setBackgroundColor(Color.parseColor("#CCCCCC"));
                textView.setText(R.string.message_sending);
            } else {
                Androids.makeText(resource.source.getMsg());
                if (resource.state == Resource.Status.SUCCESS) {
                    new TimeCount(textView).start();
                }
            }
        });
    }
}
